//开机所有段码显示闪三次
//RTD_Settings 开机后一直闪耀, 按下旋钮按键后, 显示实际温度并开始温控
//按下旋钮按键后, 显示所有设定值, 从RTD_Settings开始闪耀,没任何操作,闪5次后隐藏

//闪耀,Settings更改后延迟1s闪耀
//WIFI未连接闪耀显示


#include <stdint.h>
#include "UI.h"
#include "../MCU_Mold/MCU_Time.h"
#include "HT162X_Parts.h"
#include "HT162X_Parts_Num.h"
#include "HT162X_Pin.h"
#include "../aws_esp32/aws_iot_Publish.h"


#define UI_Shine_Count 5 //正常闪耀4次

UI_System_S UI_System;


//UI_ON: 非熄火状态 且 RTD正常
UI_State_E UI_Grill_Normal(void)
{
    if((UI_System.Grill_state != State_Grill_Misfire) && (Temp_Param.oven_Err == Pt_Normal)) return UI_ON;
    else return UI_OFF;
}

void UI_TempCtrl_Start(void)
{
    FanCtrlStart();
    UI_System.State_TempCtrl = UI_ON;
    UI_System.Grill_state = State_Grill_Ignite;
}
void UI_TempCtrl_Stop(void)
{
    if(UI_System.State_TempCtrl == UI_ON)
    {
        UI_System.State_TempCtrl = UI_OFF;
        UI_System.Grill_state = State_Grill_Misfire;
        UI_System.Timer1.Actual = 600;
        FanCtrlStop(UI_System.Timer1.Actual); //10min
    }
}

//立即退出闪耀
void UI_Shine_Exit(void)
{
    UI_System.ShineCtrl = 0;
}
void UI_Shine_AutoStart(void)
{
    UI_System.ShineCtrl = UI_Shine_Count;
}
void UI_Shine_AutoExit(void)
{
    if(UI_System.Grill_state >= State_Grill_Ignite)
    { //只有点火后才能退出闪耀
        UI_System.ShineCtrl = 0;
        UI_System.State_Operation = UI_None;

        if((UI_System.Timer1.Actual == 0) && (UI_System.Timer1.Settings == 0))
        {
            UI_MenuAuto();
        }
    }
    else UI_Shine_AutoStart();
}
void UI_Shine_Reset(void)
{
    // if(UI_System.ShineCtrl < 0xff) 
    UI_System.ShineCtrl = UI_Shine_Count + 2;
}

void UI_Encoder_KeyLong(void)
{
    if(UI_System.State_Operation != UI_None)
    {
        UI_Shine_AutoExit();
        return;
    }

    if(Temp_Param.oven_Err == Pt_Normal)
    {
        if(UI_System.Grill_state != State_Grill_Misfire) UI_TempCtrl_Stop();
        else if((UI_System.Grill_state == State_Grill_Misfire) && (UI_System.Timer1.Actual > 0))
        { //中断关机状态
            Lcd_Parts_Show();
            UI_System.Timer1.Actual = 0;
            UI_TempCtrl_Start();
        }
    }
}
void UI_Encoder_Key(void)
{
    Buzzer_Encoder_Keyboard();
    if(UI_System.Shine == Shine_All) return;

    if((UI_System.Grill_state == State_Grill_Misfire) && (UI_System.Timer1.Actual == 0))
    { //熄火状态,且倒计时归0后, 触发芯片复位
        UI_System.Grill_state = State_Grill_SetTemp;
        //关机状态重启
        UI_System.Is_Reset = UI_ON;
        UI_Init();
        UI_System.Shine = Shine_OFF;
        IO_Lcd_ON_OFF_H;
        return;
    }
    if(UI_System.Grill_state == State_Grill_Misfire) return;

    if(UI_System.State_Operation == UI_None)
    {
        if(UI_Grill_Normal() == UI_ON)
        {
            UI_System.State_Operation = UI_RTD;
            UI_System.Shine = Shine_ON;
        }
    }
    else if(UI_System.State_Operation == UI_RTD)
    {
        if(UI_System.Grill_state == State_Grill_SetTemp)
        {
            UI_Shine_Exit();
            UI_TempCtrl_Start();
            return;
        }
        else
        {
            if((UI_System.Grill_Smoke == State_Grill_Smoke) && (UI_System.ShineCtrl == 0xff))
            {
                UI_Shine_Exit();
                UI_TempCtrl_Start();
                return;
            } 
            else UI_System.State_Operation = UI_Timer_Hour;
        }
    }
    else if(UI_System.State_Operation == UI_Timer_Hour)
    {
        if(UI_System.Timer1.Settings >= 86400)
        { //24H,跳过Minute设置
            if(Temp_Param.probe1_Err == Pt_Normal) UI_System.State_Operation = UI_ProbeA;
            else if(Temp_Param.probe2_Err == Pt_Normal) UI_System.State_Operation = UI_ProbeB;
            else UI_System.State_Operation = UI_RTD;
        }
        else UI_System.State_Operation = UI_Timer_Minute;
    }
    else if(UI_System.State_Operation == UI_Timer_Minute)
    {
        if(Temp_Param.probe1_Err == Pt_Normal) UI_System.State_Operation = UI_ProbeA;
        else if(Temp_Param.probe2_Err == Pt_Normal) UI_System.State_Operation = UI_ProbeB;
        else UI_System.State_Operation = UI_RTD;

        if(UI_System.Timer1.Alart_State == Alart_Action) UI_System.Timer1.Alart_State = Alart_None;
    }
    else if(UI_System.State_Operation == UI_ProbeA)
    {
        if(Temp_Param.probe2_Err == Pt_Normal) UI_System.State_Operation = UI_ProbeB;
        else UI_System.State_Operation = UI_RTD;
        if(UI_System.ProbeA.Alart_State == Alart_Action) UI_System.ProbeA.Alart_State = Alart_None;
    }
    else if(UI_System.State_Operation == UI_ProbeB)
    {
        UI_System.State_Operation = UI_RTD;
        if(UI_System.ProbeB.Alart_State == Alart_Action) UI_System.ProbeB.Alart_State = Alart_None;
    }
    UI_Shine_AutoStart();
}
void UI_Encoder_R(void)
{
    uint16_t temp = 0;
    if(UI_System.Grill_state == State_Grill_Misfire) return;

    Buzzer_Encoder_Down();
    if(UI_System.State_Operation == UI_RTD)
    {
        if(UI_System.Grill_Smoke == State_Grill_Smoke)
        {
            UI_System.Grill_Smoke = State_Grill_Cook;

            if(UI_System.Grill_Unit == Unit_F) temp = Oven_Setting_Min_F;
            else temp = Oven_Setting_Min_C;
            UI_System.RTD.Settings = temp;
            UI_System.RTD.IsChanged = 1;
        }
        else
        {
            if(UI_System.Grill_Unit == Unit_F) temp = Oven_Setting_Max_F;
            else temp = Oven_Setting_Max_C;
            if(UI_System.RTD.Settings < temp)
            {
                UI_System.RTD.Settings += 5;
                UI_System.RTD.IsChanged = 1;
            }
        } 
    }
    else if(UI_System.State_Operation == UI_Timer_Hour)
    {
        if((UI_System.Timer1.Settings / 3600) < 24) UI_System.Timer1.Settings += 3600;
        UI_System.Timer1.IsChanged = 1;
    }
    else if(UI_System.State_Operation == UI_Timer_Minute)
    {
        if(((UI_System.Timer1.Settings / 60) % 60) < 59) UI_System.Timer1.Settings += 60;
        UI_System.Timer1.IsChanged = 1;
    }
    else if(UI_System.State_Operation == UI_ProbeA)
    {
        if(UI_System.Grill_Unit == Unit_F) temp = Probe_Setting_Max_F;
        else temp = Probe_Setting_Max_C;
        if(UI_System.ProbeA.Settings == Alarm_Probe_Min)
        {
            if(UI_System.Grill_Unit == Unit_F) UI_System.ProbeA.Settings = Probe_Setting_Min_F;
            else UI_System.ProbeA.Settings = Probe_Setting_Min_C;
            UI_System.Alarm_ProbeA = Alarm_ProbeA_Enable;
        }
        else if(UI_System.ProbeA.Settings < temp)
        {
            UI_System.ProbeA.Settings += 5;
            UI_System.Alarm_ProbeA = Alarm_ProbeA_Enable;
        }
    }
    else if(UI_System.State_Operation == UI_ProbeB)
    {
        if(UI_System.Grill_Unit == Unit_F) temp = Probe_Setting_Max_F;
        else temp = Probe_Setting_Max_C;
        if(UI_System.ProbeB.Settings == Alarm_Probe_Min)
        {
            if(UI_System.Grill_Unit == Unit_F) UI_System.ProbeB.Settings = Probe_Setting_Min_F;
            else UI_System.ProbeB.Settings = Probe_Setting_Min_C;
            UI_System.Alarm_ProbeB = Alarm_ProbeB_Enable;
        }
        else if(UI_System.ProbeB.Settings < temp)
        {
            UI_System.ProbeB.Settings += 5;
            UI_System.Alarm_ProbeB = Alarm_ProbeB_Enable;
        }
    }
    UI_Shine_Reset();
}
void UI_Encoder_L(void)
{
    uint16_t temp = 0;
    if(UI_System.Grill_state == State_Grill_Misfire) return;

    Buzzer_Encoder_Up();
    if(UI_System.State_Operation == UI_RTD)
    {
        if(UI_System.Grill_Unit == Unit_F) temp = Oven_Setting_Min_F;
        else temp = Oven_Setting_Min_C;
        if(UI_System.RTD.Settings > temp)
        {
            UI_System.RTD.Settings -= 5;
            UI_System.RTD.IsChanged = 1;
        }
        else
        {
            UI_System.Grill_Smoke = State_Grill_Smoke;
            UI_System.RTD.Settings = temp;
            UI_System.RTD.IsChanged = 1;
        }
    }
    else if(UI_System.State_Operation == UI_Timer_Hour)
    {
        if((UI_System.Timer1.Settings / 3600) > 0) UI_System.Timer1.Settings -= 3600;
        UI_System.Timer1.IsChanged = 1;
    }
    else if(UI_System.State_Operation == UI_Timer_Minute)
    {
        if(((UI_System.Timer1.Settings / 60) % 60) > 0) UI_System.Timer1.Settings -= 60;
        UI_System.Timer1.IsChanged = 1;
    }
    else if(UI_System.State_Operation == UI_ProbeA)
    {
        if(UI_System.Grill_Unit == Unit_F) temp = Probe_Setting_Min_F;
        else temp = Probe_Setting_Min_C;
        if(UI_System.ProbeA.Settings > temp)
        {
            UI_System.ProbeA.Settings -= 5;
            // UI_System.Alarm_ProbeA = Alarm_ProbeA_Enable;
        }
    }
    else if(UI_System.State_Operation == UI_ProbeB)
    {
        if(UI_System.Grill_Unit == Unit_F) temp = Probe_Setting_Min_F;
        else temp = Probe_Setting_Min_C;
        if(UI_System.ProbeB.Settings > temp)
        {
            UI_System.ProbeB.Settings -= 5;
            // UI_System.Alarm_ProbeB = Alarm_ProbeB_Enable;
        }
    }
    UI_Shine_Reset();
}

//F挡位转 C挡位
uint16_t Get_C_Gear(uint16_t F, uint16_t minCGear, uint16_t minFGear)
{
    float Seq = 0;
    float temp = 0;
    float tempMinF = 0;
    uint16_t newSeq = 0;
    uint16_t C = 0;

    temp = F;
    tempMinF = minFGear;
    Seq = (temp - tempMinF)/5.0;
    temp = (Seq / 1.8125); //直接丢弃小数,整数不加1
    newSeq = temp;
    C = minCGear + newSeq*5; //C挡位,增量5

    // printf("Get_C_Gear: %d %d %f %d %f %d\n",F, minFGear, Seq, newSeq, temp, C);
    return C;
}
//C挡位转 F挡位
uint16_t Get_F_Gear(uint16_t C, uint16_t minFGear, uint16_t minCGear)
{
    float Seq = 0;
    float temp = 0;
    float tempMinC = 0;
    uint16_t newSeq = 0;
    uint16_t F = 0;

    temp = C;
    tempMinC = minCGear;
    Seq = (temp - tempMinC) / 5.0;
    Seq = Seq*1.8125;
    newSeq = Seq; //丢弃小数，取整数
    temp = Seq - newSeq; //取小数
    if(temp > 0) newSeq += 1; //只要小数>0,整数+1
    F = newSeq * 5 + minFGear; //F挡位, 增量5

    // printf("Get_F_Gear: %d %d %f %d %f %d\n",C, minCGear, Seq, newSeq, temp, F);
    return F;
}

//10ms
void UI_Clock(void)
{
    static uint8_t x = 0;
    static uint8_t x2 = 0;
    static uint8_t x3 = 0;
    static uint8_t x4 = 0;

    x4++;
    if(x4 > 40)
    { //400ms
        x4 = 0;
        if(UI_System.Shine_WIFI == Shine_ON) UI_System.Shine_WIFI = Shine_OFF;
        else UI_System.Shine_WIFI = Shine_ON;
    }

    if(Factory_Enable > 0) return;
    if(UI_System.Shine == Shine_All) return;
    if(UI_System.State_Operation == UI_None)
    {
        if(x > 0) x = 0;
        x3++;
        if(x3 >= 50)
        { //500ms
            x3 = 0;
            if(UI_System.Shine_Colon == Shine_ON) UI_System.Shine_Colon = Shine_OFF;
            else UI_System.Shine_Colon = Shine_ON;
        }
    }
    x2++;
    if(x2 >= 100)
    { //1s
        x2 = 0;
        if(UI_System.Timer1.Actual > 86400)
        {
            UI_System.Timer1.Actual = 86400;
            UI_System.Timer1.Settings = 86400;
        }
        if(UI_System.Timer1.Actual > 0)
        {
            UI_System.Timer1.Actual--;
            if(UI_System.Timer1.Actual == 0)
            {
                UI_System.Alarm_Timer = Alarm_Timer1; //Timer Alarm Enable
                UI_System.Timer1.Settings = 0;
                UI_System.Timer1.Alart_State = Alart_Action;

                UI_MenuAuto();
            }
        }
    }

    if(UI_System.ShineCtrl > 0)
    {
        if(x >= 50)
        {
            x = 0;
            if(UI_System.Shine == Shine_OFF)
            {
                UI_System.Shine = Shine_ON;
                if(UI_System.ShineCtrl < 0xff) UI_System.ShineCtrl--;
            }
            else
            {
                if(UI_System.ShineCtrl <= UI_Shine_Count) UI_System.Shine = Shine_OFF;
                else if(UI_System.ShineCtrl < 0xff) UI_System.ShineCtrl--;
                else UI_System.Shine = Shine_OFF;
            }
            if(UI_System.ShineCtrl == 0)
            { //闪耀退出后执行 一次
                if(UI_System.Timer1.IsChanged > 0)
                {
                    UI_System.Timer1.IsChanged = 0;
                    UI_System.Timer1.Actual = UI_System.Timer1.Settings;
                }
                if(UI_System.RTD.IsChanged > 0)
                {
                    UI_System.RTD.IsChanged = 0;
                    ADC_KeyUpDown();
                }
            }
        }
        else x++;
    }
    else UI_Shine_AutoExit();
}

//程序烧录后，默认显示 F
void UI_Default_F(void)
{
    UI_System.Grill_Unit = Unit_F;
    UI_System.RTD.Settings = Oven_Setting_Min_F;
    UI_System.ProbeA.Settings = Probe_Setting_Max_F;
    UI_System.ProbeB.Settings = Probe_Setting_Max_F;
}
//程序烧录后，默认显示 C
void UI_Default_C(void)
{
    UI_System.Grill_Unit = Unit_C;
    UI_System.RTD.Settings = Oven_Setting_Min_C;
    UI_System.ProbeA.Settings = Probe_Setting_Max_C;
    UI_System.ProbeB.Settings = Probe_Setting_Max_C;
}

void UI_Init(void)
{
    UI_System.Shine = Shine_All;
    UI_System.State_TempCtrl = UI_OFF;
    UI_System.ICO_WIFI = UI_OFF;
    UI_System.ICO_Bluetooth = UI_ON;
    UI_System.ICO_Auger = UI_OFF;
    UI_System.ICO_Fan = UI_OFF;
    UI_System.ICO_Heating = UI_OFF;
    UI_System.ICO_Error = UI_OFF;
    UI_System.ICO_Error_Auger = UI_OFF;
    UI_System.ICO_Error_Fan = UI_OFF;
    UI_System.ICO_Error_Ignite = UI_OFF;
    UI_System.ICO_Cell = UI_OFF;
    UI_System.Grill_state = State_Grill_SetTemp;
    UI_System.State_Operation = UI_None;
    UI_System.ShineCtrl = 0;

    UI_System.Timer1.Actual = 0;
    UI_System.Timer1.Settings = 0;

    UI_System.Alarm_Timer = Alarm_None;
    UI_System.Alarm_RTD = Alarm_None;
    UI_System.Alarm_ProbeA = Alarm_None;
    UI_System.Alarm_ProbeB = Alarm_None;
    
    UI_Default_C();

    AWS_IoT_PublishMsg.PubMsg_RTD_Set[1] = 0;
    AWS_IoT_PublishMsg.PubMsg_RTD_Set[2] = 0;
    Lcd_Parts_Show();
}
void UI_Main(void)
{
    extern volatile unsigned int firmware_number; //固件版本号

    HT162X_RAM_Tx();
    if(UI_System.Shine == Shine_All) return;
    if(UI_System.Shine == Shine_Init)
    {
        Lcd_Parts_Refresh();
        Lcd_NumShow(UI_RTDSettings,UI_System.RTD.Settings);
        UI_System.State_Operation = UI_RTD;
        UI_System.ShineCtrl = 0xff; //一直闪耀
        UI_System.Shine = Shine_ON;
    }
    else if(UI_System.State_Operation == UI_None)
    { //更新 设置温度, 正常工作界面(非用户操作界面)
        if(temp_set != UI_System.RTD.Settings)
        {
            temp_set = UI_System.RTD.Settings;
        }

        if((UI_System.Alarm_ProbeA == Alarm_ProbeA_Enable) && (UI_System.ProbeA.Actual >= UI_System.ProbeA.Settings) && (UI_System.ProbeA.Settings > Alarm_Probe_Min))
        {
            UI_System.Alarm_ProbeA = Alarm_ProbeA_Action;
            UI_System.ProbeA.Settings = Alarm_Probe_Min;
            UI_System.ProbeA.Alart_State = Alart_Action;
        }
        if((UI_System.Alarm_ProbeB == Alarm_ProbeB_Enable) && (UI_System.ProbeB.Actual >= UI_System.ProbeB.Settings) && (UI_System.ProbeB.Settings > Alarm_Probe_Min))
        {
            UI_System.Alarm_ProbeB = Alarm_ProbeB_Action;
            UI_System.ProbeB.Settings = Alarm_Probe_Min;
            UI_System.ProbeB.Alart_State = Alart_Action;
        }
    }
    if(UI_System.RTD.Actual < UI_System.RTD.Settings)
    {
        if(UI_System.Grill_state == State_Grill_Ignite) UI_System.Grill_state = State_Grill_PreHeat;
    }
    else
    {
        if((UI_System.Grill_state == State_Grill_PreHeat) || (UI_System.Grill_state == State_Grill_Ignite)) UI_System.Grill_state = State_Grill_Cook;
    }

    if(UI_System.Grill_state == State_Grill_Misfire)
    { //熄火状态 Clearn the all of alarm flag
        UI_System.Alarm_Timer = Alarm_None;
        UI_System.Alarm_RTD = Alarm_None;
        UI_System.Alarm_ProbeA = Alarm_None;
        UI_System.Alarm_ProbeB = Alarm_None;
    }
    if(UI_System.Alarm_Timer == Alarm_Timer1)
    {
        Buzzer_Alarm();
        UI_System.Alarm_Timer = Alarm_None;
    }
    else if(UI_System.Alarm_ProbeA == Alarm_ProbeA_Action)
    {
        Buzzer_Alarm();
        UI_System.Alarm_ProbeA = Alarm_None;
    }
    else if(UI_System.Alarm_ProbeB == Alarm_ProbeB_Action)
    {
        Buzzer_Alarm();
        UI_System.Alarm_ProbeB = Alarm_None;
    }

    Lcd_Parts_Refresh();

    AWS_IoT_PublishMsg.PubMsg_Firmware = firmware_number;
    AWS_IoT_PublishMsg.PubMsg_RTD_Set[0] = UI_System.RTD.Settings;
    AWS_IoT_PublishMsg.PubMsg_RTD_Set[1] = UI_System.ProbeA.Settings;
    AWS_IoT_PublishMsg.PubMsg_RTD_Set[2] = UI_System.ProbeB.Settings;
    AWS_IoT_PublishMsg.PubMsg_RTD_Temp[0] = UI_System.RTD.Actual;
    if(Temp_Param.probe1_Err == Pt_Normal) AWS_IoT_PublishMsg.PubMsg_RTD_Temp[1] = UI_System.ProbeA.Actual;
    else AWS_IoT_PublishMsg.PubMsg_RTD_Temp[1] = 999;
    if(Temp_Param.probe2_Err == Pt_Normal) AWS_IoT_PublishMsg.PubMsg_RTD_Temp[2] = UI_System.ProbeB.Actual;
    else AWS_IoT_PublishMsg.PubMsg_RTD_Temp[2] = 999;

    AWS_IoT_PublishMsg.PubMsg_Timer_Rem[0] = UI_System.Timer1.Actual;
    AWS_IoT_PublishMsg.PubMsg_Timer_Set[0] = UI_System.Timer1.Settings;
    AWS_IoT_PublishMsg.PubMsg_Unit = UI_System.Grill_Unit;
    AWS_IoT_PublishMsg.PubMsg_Err = RTD[RTD_Seq_RTD].status;
    if(UI_System.Grill_Smoke == State_Grill_Smoke) AWS_IoT_PublishMsg.PubMsg_State = State_Grill_Smoke;
    else AWS_IoT_PublishMsg.PubMsg_State = UI_System.Grill_state;
    if(UI_System.Grill_state == State_Grill_Misfire) AWS_IoT_PublishMsg.PubMsg_Coundown = UI_System.Timer1.Actual;
}
